{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 353,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "指标\n",
       "居民人均消费支出(元)              24100.0\n",
       "居民人均消费支出比上年增长(%)            12.6\n",
       "居民人均服务性消费支出(元)           10645.0\n",
       "居民人均服务性消费支出比上年增长(%)         17.8\n",
       "居民人均食品烟酒支出(元)             7178.0\n",
       "居民人均食品烟酒支出比上年增长(%)           NaN\n",
       "居民人均衣着支出(元)               1419.0\n",
       "居民人均衣着支出比上年增长(%)             NaN\n",
       "居民人均居住支出(元)               5641.0\n",
       "居民人均居住支出比上年增长(%)             NaN\n",
       "居民人均生活用品及服务支出(元)          1423.0\n",
       "居民人均生活用品及服务支出比上年增长(%)        NaN\n",
       "居民人均交通通信支出(元)             3156.0\n",
       "居民人均交通通信支出比上年增长(%)           NaN\n",
       "居民人均教育文化娱乐支出(元)           2599.0\n",
       "居民人均教育文化娱乐支出比上年增长(%)         NaN\n",
       "居民人均医疗保健支出(元)             2115.0\n",
       "居民人均医疗保健支出比上年增长(%)           NaN\n",
       "居民人均其他用品及服务支出(元)           569.0\n",
       "居民人均其他用品及服务支出比上年增长(%)        NaN\n",
       "Name: 2021年, dtype: float64"
      ]
     },
     "execution_count": 353,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#Apply进行数据预处理\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "df1 = pd.read_csv('年度数据.csv',index_col=0,encoding='gb2312')\n",
    "df1['2021年']\n",
    "#df1['2021年'].apply(lambda x:x*2 ) #对单个元素进行处理的元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 354,
   "metadata": {},
   "outputs": [],
   "source": [
    "#df1['2021年'].apply(np.log)\n",
    "#df1['2021年'].apply(lambda x: x/7)\n",
    "#def koo(x):\n",
    "#    return x/7\n",
    "#df1.apply(koo)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 355,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>2021年</th>\n",
       "      <th>2020年</th>\n",
       "      <th>2019年</th>\n",
       "      <th>2018年</th>\n",
       "      <th>2017年</th>\n",
       "      <th>2016年</th>\n",
       "      <th>2015年</th>\n",
       "      <th>2014年</th>\n",
       "      <th>2013年</th>\n",
       "      <th>2012年</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>指标</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>居民人均消费支出(元)</th>\n",
       "      <td>24100.0</td>\n",
       "      <td>21210.0</td>\n",
       "      <td>21559.0</td>\n",
       "      <td>19853.0</td>\n",
       "      <td>18322.0</td>\n",
       "      <td>17111.0</td>\n",
       "      <td>15712.0</td>\n",
       "      <td>14491.0</td>\n",
       "      <td>13220.0</td>\n",
       "      <td>12054.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>居民人均消费支出比上年增长(%)</th>\n",
       "      <td>12.6</td>\n",
       "      <td>-4.0</td>\n",
       "      <td>5.5</td>\n",
       "      <td>6.2</td>\n",
       "      <td>5.4</td>\n",
       "      <td>6.8</td>\n",
       "      <td>6.9</td>\n",
       "      <td>7.5</td>\n",
       "      <td>6.9</td>\n",
       "      <td>8.6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>居民人均服务性消费支出(元)</th>\n",
       "      <td>10645.0</td>\n",
       "      <td>9037.0</td>\n",
       "      <td>9886.0</td>\n",
       "      <td>8781.0</td>\n",
       "      <td>7803.0</td>\n",
       "      <td>7157.0</td>\n",
       "      <td>6460.0</td>\n",
       "      <td>5842.0</td>\n",
       "      <td>5246.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>居民人均服务性消费支出比上年增长(%)</th>\n",
       "      <td>17.8</td>\n",
       "      <td>-8.6</td>\n",
       "      <td>12.6</td>\n",
       "      <td>12.5</td>\n",
       "      <td>9.0</td>\n",
       "      <td>10.8</td>\n",
       "      <td>10.6</td>\n",
       "      <td>11.4</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>居民人均食品烟酒支出(元)</th>\n",
       "      <td>7178.0</td>\n",
       "      <td>6397.0</td>\n",
       "      <td>6084.0</td>\n",
       "      <td>5631.0</td>\n",
       "      <td>5374.0</td>\n",
       "      <td>5151.0</td>\n",
       "      <td>4814.0</td>\n",
       "      <td>4494.0</td>\n",
       "      <td>4127.0</td>\n",
       "      <td>3983.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>居民人均衣着支出(元)</th>\n",
       "      <td>1419.0</td>\n",
       "      <td>1238.0</td>\n",
       "      <td>1338.0</td>\n",
       "      <td>1289.0</td>\n",
       "      <td>1238.0</td>\n",
       "      <td>1203.0</td>\n",
       "      <td>1164.0</td>\n",
       "      <td>1099.0</td>\n",
       "      <td>1027.0</td>\n",
       "      <td>992.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>居民人均居住支出(元)</th>\n",
       "      <td>5641.0</td>\n",
       "      <td>5215.0</td>\n",
       "      <td>5055.0</td>\n",
       "      <td>4647.0</td>\n",
       "      <td>4107.0</td>\n",
       "      <td>3746.0</td>\n",
       "      <td>3419.0</td>\n",
       "      <td>3201.0</td>\n",
       "      <td>2999.0</td>\n",
       "      <td>2480.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>居民人均生活用品及服务支出(元)</th>\n",
       "      <td>1423.0</td>\n",
       "      <td>1260.0</td>\n",
       "      <td>1281.0</td>\n",
       "      <td>1223.0</td>\n",
       "      <td>1121.0</td>\n",
       "      <td>1044.0</td>\n",
       "      <td>951.0</td>\n",
       "      <td>890.0</td>\n",
       "      <td>806.0</td>\n",
       "      <td>741.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>居民人均交通通信支出(元)</th>\n",
       "      <td>3156.0</td>\n",
       "      <td>2762.0</td>\n",
       "      <td>2862.0</td>\n",
       "      <td>2675.0</td>\n",
       "      <td>2499.0</td>\n",
       "      <td>2338.0</td>\n",
       "      <td>2087.0</td>\n",
       "      <td>1869.0</td>\n",
       "      <td>1627.0</td>\n",
       "      <td>1451.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>居民人均教育文化娱乐支出(元)</th>\n",
       "      <td>2599.0</td>\n",
       "      <td>2032.0</td>\n",
       "      <td>2513.0</td>\n",
       "      <td>2226.0</td>\n",
       "      <td>2086.0</td>\n",
       "      <td>1915.0</td>\n",
       "      <td>1723.0</td>\n",
       "      <td>1536.0</td>\n",
       "      <td>1398.0</td>\n",
       "      <td>1262.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>居民人均医疗保健支出(元)</th>\n",
       "      <td>2115.0</td>\n",
       "      <td>1843.0</td>\n",
       "      <td>1902.0</td>\n",
       "      <td>1685.0</td>\n",
       "      <td>1451.0</td>\n",
       "      <td>1307.0</td>\n",
       "      <td>1165.0</td>\n",
       "      <td>1045.0</td>\n",
       "      <td>912.0</td>\n",
       "      <td>838.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>居民人均其他用品及服务支出(元)</th>\n",
       "      <td>569.0</td>\n",
       "      <td>462.0</td>\n",
       "      <td>524.0</td>\n",
       "      <td>477.0</td>\n",
       "      <td>447.0</td>\n",
       "      <td>406.0</td>\n",
       "      <td>389.0</td>\n",
       "      <td>358.0</td>\n",
       "      <td>325.0</td>\n",
       "      <td>307.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>居民人均其他用品及服务支出比上年增长(%)</th>\n",
       "      <td>NaN</td>\n",
       "      <td>-11.8</td>\n",
       "      <td>9.7</td>\n",
       "      <td>6.8</td>\n",
       "      <td>10.0</td>\n",
       "      <td>4.4</td>\n",
       "      <td>8.7</td>\n",
       "      <td>10.3</td>\n",
       "      <td>5.6</td>\n",
       "      <td>12.9</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                         2021年    2020年    2019年    2018年    2017年    2016年  \\\n",
       "指标                                                                            \n",
       "居民人均消费支出(元)            24100.0  21210.0  21559.0  19853.0  18322.0  17111.0   \n",
       "居民人均消费支出比上年增长(%)          12.6     -4.0      5.5      6.2      5.4      6.8   \n",
       "居民人均服务性消费支出(元)         10645.0   9037.0   9886.0   8781.0   7803.0   7157.0   \n",
       "居民人均服务性消费支出比上年增长(%)       17.8     -8.6     12.6     12.5      9.0     10.8   \n",
       "居民人均食品烟酒支出(元)           7178.0   6397.0   6084.0   5631.0   5374.0   5151.0   \n",
       "居民人均衣着支出(元)             1419.0   1238.0   1338.0   1289.0   1238.0   1203.0   \n",
       "居民人均居住支出(元)             5641.0   5215.0   5055.0   4647.0   4107.0   3746.0   \n",
       "居民人均生活用品及服务支出(元)        1423.0   1260.0   1281.0   1223.0   1121.0   1044.0   \n",
       "居民人均交通通信支出(元)           3156.0   2762.0   2862.0   2675.0   2499.0   2338.0   \n",
       "居民人均教育文化娱乐支出(元)         2599.0   2032.0   2513.0   2226.0   2086.0   1915.0   \n",
       "居民人均医疗保健支出(元)           2115.0   1843.0   1902.0   1685.0   1451.0   1307.0   \n",
       "居民人均其他用品及服务支出(元)         569.0    462.0    524.0    477.0    447.0    406.0   \n",
       "居民人均其他用品及服务支出比上年增长(%)      NaN    -11.8      9.7      6.8     10.0      4.4   \n",
       "\n",
       "                         2015年    2014年    2013年    2012年  \n",
       "指标                                                         \n",
       "居民人均消费支出(元)            15712.0  14491.0  13220.0  12054.0  \n",
       "居民人均消费支出比上年增长(%)           6.9      7.5      6.9      8.6  \n",
       "居民人均服务性消费支出(元)          6460.0   5842.0   5246.0      NaN  \n",
       "居民人均服务性消费支出比上年增长(%)       10.6     11.4      NaN      NaN  \n",
       "居民人均食品烟酒支出(元)           4814.0   4494.0   4127.0   3983.0  \n",
       "居民人均衣着支出(元)             1164.0   1099.0   1027.0    992.0  \n",
       "居民人均居住支出(元)             3419.0   3201.0   2999.0   2480.0  \n",
       "居民人均生活用品及服务支出(元)         951.0    890.0    806.0    741.0  \n",
       "居民人均交通通信支出(元)           2087.0   1869.0   1627.0   1451.0  \n",
       "居民人均教育文化娱乐支出(元)         1723.0   1536.0   1398.0   1262.0  \n",
       "居民人均医疗保健支出(元)           1165.0   1045.0    912.0    838.0  \n",
       "居民人均其他用品及服务支出(元)         389.0    358.0    325.0    307.0  \n",
       "居民人均其他用品及服务支出比上年增长(%)      8.7     10.3      5.6     12.9  "
      ]
     },
     "execution_count": 355,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#数据的去重 Series的unique方法\n",
    "#df1['2021年'].unique() #查看独特的\n",
    "df1['2021年'].duplicated() #查看是否为重复，显示True或False\n",
    "df1.drop_duplicates(['2021年'],keep=\"last\") #扔掉重复项"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 356,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2012-09-02   -0.056765\n",
       "2012-10-02   -1.197001\n",
       "2042-10-02    0.418971\n",
       "2012-10-22   -0.738083\n",
       "dtype: float64"
      ]
     },
     "execution_count": 356,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#处理时间序列\n",
    "from datetime import datetime\n",
    "t1 = datetime(2022,10,2)\n",
    "datelist = [datetime(2012,9,2)\n",
    "            ,datetime(2012,10,2)\n",
    "            ,datetime(2042,10,2)\n",
    "            ,datetime(2012,10,22)]\n",
    "s1 = pd.Series(np.random.randn(4),index=datelist)\n",
    "s1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 357,
   "metadata": {},
   "outputs": [],
   "source": [
    "#s1[datetime(2022,10,2)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 358,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Series([], dtype: float64)"
      ]
     },
     "execution_count": 358,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s1['2022-10-2']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 359,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2012-10-22   -0.738083\n",
       "dtype: float64"
      ]
     },
     "execution_count": 359,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s1['20121022']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 360,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-1.9350841527999147"
      ]
     },
     "execution_count": 360,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s1['2012-10'].sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 361,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "指标\n",
       "居民人均消费支出(元)                             NaN\n",
       "居民人均消费支出比上年增长(%)                (0.0, 50.0]\n",
       "居民人均服务性消费支出(元)           (10000.0, 20000.0]\n",
       "居民人均服务性消费支出比上年增长(%)             (0.0, 50.0]\n",
       "居民人均食品烟酒支出(元)             (5000.0, 10000.0]\n",
       "居民人均食品烟酒支出比上年增长(%)                      NaN\n",
       "居民人均衣着支出(元)                  (50.0, 5000.0]\n",
       "居民人均衣着支出比上年增长(%)                        NaN\n",
       "居民人均居住支出(元)               (5000.0, 10000.0]\n",
       "居民人均居住支出比上年增长(%)                        NaN\n",
       "居民人均生活用品及服务支出(元)             (50.0, 5000.0]\n",
       "居民人均生活用品及服务支出比上年增长(%)                   NaN\n",
       "居民人均交通通信支出(元)                (50.0, 5000.0]\n",
       "居民人均交通通信支出比上年增长(%)                      NaN\n",
       "居民人均教育文化娱乐支出(元)              (50.0, 5000.0]\n",
       "居民人均教育文化娱乐支出比上年增长(%)                    NaN\n",
       "居民人均医疗保健支出(元)                (50.0, 5000.0]\n",
       "居民人均医疗保健支出比上年增长(%)                      NaN\n",
       "居民人均其他用品及服务支出(元)             (50.0, 5000.0]\n",
       "居民人均其他用品及服务支出比上年增长(%)                   NaN\n",
       "Name: 2021年, dtype: category\n",
       "Categories (4, interval[int64, right]): [(0, 50] < (50, 5000] < (5000, 10000] < (10000, 20000]]"
      ]
     },
     "execution_count": 361,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#数据分箱技术Binning\n",
    "bins = [0,50,5000,10000,20000]\n",
    "category = pd.cut(df1['2021年'],bins)\n",
    "category"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 362,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(50, 5000]        6\n",
       "(0, 50]           2\n",
       "(5000, 10000]     2\n",
       "(10000, 20000]    1\n",
       "Name: 2021年, dtype: int64"
      ]
     },
     "execution_count": 362,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.value_counts(category)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 363,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>date</th>\n",
       "      <th>city</th>\n",
       "      <th>temperature</th>\n",
       "      <th>wind</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>03/01/2016</td>\n",
       "      <td>BJ</td>\n",
       "      <td>8</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>17/01/2016</td>\n",
       "      <td>BJ</td>\n",
       "      <td>12</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>31/01/2016</td>\n",
       "      <td>BJ</td>\n",
       "      <td>19</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>14/02/2016</td>\n",
       "      <td>BJ</td>\n",
       "      <td>-3</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>28/02/2016</td>\n",
       "      <td>BJ</td>\n",
       "      <td>19</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>13/03/2016</td>\n",
       "      <td>BJ</td>\n",
       "      <td>5</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>27/03/2016</td>\n",
       "      <td>SH</td>\n",
       "      <td>-4</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>10/04/2016</td>\n",
       "      <td>SH</td>\n",
       "      <td>19</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>24/04/2016</td>\n",
       "      <td>SH</td>\n",
       "      <td>20</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>08/05/2016</td>\n",
       "      <td>SH</td>\n",
       "      <td>17</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>22/05/2016</td>\n",
       "      <td>SH</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>05/06/2016</td>\n",
       "      <td>SH</td>\n",
       "      <td>-10</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>19/06/2016</td>\n",
       "      <td>SH</td>\n",
       "      <td>0</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>03/07/2016</td>\n",
       "      <td>SH</td>\n",
       "      <td>-9</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>17/07/2016</td>\n",
       "      <td>GZ</td>\n",
       "      <td>10</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>31/07/2016</td>\n",
       "      <td>GZ</td>\n",
       "      <td>-1</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>14/08/2016</td>\n",
       "      <td>GZ</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>28/08/2016</td>\n",
       "      <td>GZ</td>\n",
       "      <td>25</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>11/09/2016</td>\n",
       "      <td>SZ</td>\n",
       "      <td>20</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>25/09/2016</td>\n",
       "      <td>SZ</td>\n",
       "      <td>-10</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          date city  temperature  wind\n",
       "0   03/01/2016   BJ            8     5\n",
       "1   17/01/2016   BJ           12     2\n",
       "2   31/01/2016   BJ           19     2\n",
       "3   14/02/2016   BJ           -3     3\n",
       "4   28/02/2016   BJ           19     2\n",
       "5   13/03/2016   BJ            5     3\n",
       "6   27/03/2016   SH           -4     4\n",
       "7   10/04/2016   SH           19     3\n",
       "8   24/04/2016   SH           20     3\n",
       "9   08/05/2016   SH           17     3\n",
       "10  22/05/2016   SH            4     2\n",
       "11  05/06/2016   SH          -10     4\n",
       "12  19/06/2016   SH            0     5\n",
       "13  03/07/2016   SH           -9     5\n",
       "14  17/07/2016   GZ           10     2\n",
       "15  31/07/2016   GZ           -1     5\n",
       "16  14/08/2016   GZ            1     5\n",
       "17  28/08/2016   GZ           25     4\n",
       "18  11/09/2016   SZ           20     1\n",
       "19  25/09/2016   SZ          -10     4"
      ]
     },
     "execution_count": 363,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#数据分组技术groupby\n",
    "df1 = pd.read_csv('city_weather.csv')\n",
    "df1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 364,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<pandas.core.groupby.generic.DataFrameGroupBy object at 0x0B621BB0>"
      ]
     },
     "execution_count": 364,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "g = df1.groupby(df1['city'])#返回对象拿一个新的做类比\n",
    "g"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 365,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'BJ': [0, 1, 2, 3, 4, 5], 'GZ': [14, 15, 16, 17], 'SH': [6, 7, 8, 9, 10, 11, 12, 13], 'SZ': [18, 19]}"
      ]
     },
     "execution_count": 365,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "g.groups"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 366,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>date</th>\n",
       "      <th>city</th>\n",
       "      <th>temperature</th>\n",
       "      <th>wind</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>03/01/2016</td>\n",
       "      <td>BJ</td>\n",
       "      <td>8</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>17/01/2016</td>\n",
       "      <td>BJ</td>\n",
       "      <td>12</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>31/01/2016</td>\n",
       "      <td>BJ</td>\n",
       "      <td>19</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>14/02/2016</td>\n",
       "      <td>BJ</td>\n",
       "      <td>-3</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>28/02/2016</td>\n",
       "      <td>BJ</td>\n",
       "      <td>19</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>13/03/2016</td>\n",
       "      <td>BJ</td>\n",
       "      <td>5</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         date city  temperature  wind\n",
       "0  03/01/2016   BJ            8     5\n",
       "1  17/01/2016   BJ           12     2\n",
       "2  31/01/2016   BJ           19     2\n",
       "3  14/02/2016   BJ           -3     3\n",
       "4  28/02/2016   BJ           19     2\n",
       "5  13/03/2016   BJ            5     3"
      ]
     },
     "execution_count": 366,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dfb = g.get_group('BJ')\n",
    "dfb"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 367,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>date</th>\n",
       "      <th>temperature</th>\n",
       "      <th>wind</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>city</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>BJ</th>\n",
       "      <td>31/01/2016</td>\n",
       "      <td>19</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>GZ</th>\n",
       "      <td>31/07/2016</td>\n",
       "      <td>25</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>SH</th>\n",
       "      <td>27/03/2016</td>\n",
       "      <td>20</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>SZ</th>\n",
       "      <td>25/09/2016</td>\n",
       "      <td>20</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            date  temperature  wind\n",
       "city                               \n",
       "BJ    31/01/2016           19     5\n",
       "GZ    31/07/2016           25     5\n",
       "SH    27/03/2016           20     5\n",
       "SZ    25/09/2016           20     4"
      ]
     },
     "execution_count": 367,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "g.max()\n",
    "#g.count()\n",
    "#g.describe()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 369,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>temperature</th>\n",
       "      <th>wind</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>city</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>BJ</th>\n",
       "      <td>60</td>\n",
       "      <td>17</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>GZ</th>\n",
       "      <td>35</td>\n",
       "      <td>16</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>SH</th>\n",
       "      <td>37</td>\n",
       "      <td>29</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>SZ</th>\n",
       "      <td>10</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      temperature  wind\n",
       "city                   \n",
       "BJ             60    17\n",
       "GZ             35    16\n",
       "SH             37    29\n",
       "SZ             10     5"
      ]
     },
     "execution_count": 369,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#数据聚合技术\n",
    "g.agg(np.sum) #min"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def foo(attr):\n",
    "    # print(type(attr))\n",
    "    return attr.max()*2\n",
    "\n",
    "g['wind'].agg(foo)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 371,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>temperature</th>\n",
       "      <th>wind</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>city</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>BJ</th>\n",
       "      <td>60</td>\n",
       "      <td>17</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>GZ</th>\n",
       "      <td>35</td>\n",
       "      <td>16</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>SH</th>\n",
       "      <td>37</td>\n",
       "      <td>29</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>SZ</th>\n",
       "      <td>10</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      temperature  wind\n",
       "city                   \n",
       "BJ             60    17\n",
       "GZ             35    16\n",
       "SH             37    29\n",
       "SZ             10     5"
      ]
     },
     "execution_count": 371,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#透视表\n",
    "pd.pivot_table(df1,index=['city'],aggfunc='sum')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
